<?php
namespace app\admin\controller;
class Agent extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function agentList() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        if($this->role != 1){
            return errorResult();
        }
        
        
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        $offset = ($page - 1) * $pageSize;
        
        $where = [];
        
        $agentType = isset($params['agentType']) ? intval($params['agentType']) : 0;
        if(in_array($agentType, [1, 2])) {
            $where[] = ['agent_type', '=', $agentType];
        }
        
        $agentName = isset($params['agentName']) ? trim($params['agentName']) : '';
        if($agentName !== '') {
            $where[] = ['agent_name' , 'like', '%' . $agentName . '%'];
        }
        
        $agentAccount = isset($params['agentAccount']) ? trim($params['agentAccount']) : '';
        if($agentAccount !== '') {
            $where[] = ['agent_account' , 'like', '%' . $agentAccount . '%'];
        }
        
        $repData = [];
        $model = model('Agent');
        
        $repData['list'] = $model
            ->where($where)
            ->field(['agent_id', 'agent_name', 'agent_type', 'agent_account', 'contact', 'mobile', 'service_telephone', 'total_amount', 'profit_amount', 'profit_online_amount', 'profit_coin_amount', 'profit_card_amount', 'create_time'])
            ->order('agent_id desc')
            ->limit($offset, $pageSize)
            ->select();
            
        if(isset($params['init']) && $params['init'] == 1){
            $repData['total'] = $model->where($where)->count();
        }
        
        return successResult($repData);
        
    }
    
    public function save() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        if($this->role != 1){
            return errorResult();
        }
        
        
        
        $params = request()->post();
        
        $agentId = isset($params['agent_id']) ? intval($params['agent_id']) : 0;
        
        $data = [];
        
        $data['agent_account'] = isset($params['agent_account']) ? trim($params['agent_account']) : '';
        if($data['agent_account'] === ''){
            return errorResult('请设置代理商登录账号');
        }
        
        $data['agent_name'] = isset($params['agent_name']) ? trim($params['agent_name']) : '';
        if($data['agent_name'] === ''){
            return errorResult('请填写代理商名称');
        }
        
        $data['agent_type'] = isset($params['agent_type']) ? intval($params['agent_type']) : 1;
        if($data['agent_type'] === 2){ //特约代理商
            $data['mch_id'] = isset($params['mch_id']) ? trim($params['mch_id']) : '';
            if($data['mch_id'] === ''){
                return errorResult('请填写微信商户id');
            }
            
            $data['mch_key'] = isset($params['mch_key']) ? trim($params['mch_key']) : '';
            if($data['mch_key'] === ''){
                return errorResult('请填写微信商户key');
            }
        }

        
        $data['contact'] = isset($params['contact']) ? trim($params['contact']) : '';
        $data['mobile'] = isset($params['mobile']) ? trim($params['mobile']) : '';
        $data['service_telephone'] = isset($params['service_telephone']) ? trim($params['service_telephone']) : '';
        $data['withdra_rate'] = isset($params['withdra_rate']) ? floatval($params['withdra_rate']) : 0;
        if($data['withdra_rate'] < 0 || $data['withdra_rate'] > 100){
            return errorResult('提现费率只能在0-100之间');
        }
        
        $data['province_id'] = isset($params['province_id']) ? intval($params['province_id']) : 0; //
        $data['city_id'] = isset($params['city_id']) ? intval($params['city_id']) : 0; //
        $data['district_id'] = isset($params['district_id']) ? intval($params['district_id']) : 0; //
        $data['address'] = isset($params['address']) ? trim($params['address']) : ''; //
        
        $data['status'] = isset($params['status']) ? intval($params['status']) : 1; //
        
        if($data['province_id'] > 0){
            $areaModel = model('Area');
            $data['province'] = $areaModel->where(['id' => $data['province_id']])->value('name');
            if($data['city_id'] > 0){
                $data['city'] = $areaModel->where(['id' => $data['city_id']])->value('name');
                if($data['district_id'] > 0){
                    $data['district'] = $areaModel->where(['id' => $data['district_id']])->value('name');
                }
            }
        }

        
        $agentModel = model('Agent');
        
        if($agentId > 0){
            $field = array_keys($data);
            $agent = $agentModel->findOne(['agent_id' => $agentId], $field);
            if(empty($agent)) {
                return errorResult();
            }
            
            $updateData = [];
            
            foreach ($data as $k => $val) {
                if($agent[$k] != $val){
                    $updateData[$k] = $val;
                }
            }
            
            if(empty($updateData)) {
                return successResult('编辑成功1');
            }
            
            if(isset($updateData['agent_account'])){
                $rs = $agentModel->findOne(['agent_account' => $data['agent_account']], ['agent_id']);
                if(!empty($rs)) {
                    return errorResult('已存在相同的账号');
                }
            }
            
            $rs = $agentModel->modify(['agent_id' => $agentId], $updateData);
            if(!$rs) {
                return errorResult('编辑代理商失败');
            }
            
            return successResult('编辑代理商成功');
            
        }else{
            $agent = $agentModel->findOne(['agent_account' => $data['agent_account']], ['agent_id']);
            if(!empty($agent)) {
                return errorResult('已存在相同的账号');
            }
            
            $salt = uniqid();
            $salt = substr($salt, strlen($salt) -7 , 6);
            $password = md5(md5(md5('123456')) . $salt);
            
            $data['salt'] = $salt;
            $data['password'] = $password;
            $data['create_time'] = time();
            
            $agentId = $agentModel->add($data);
            if(!$agentId){
                return errorResult('新增代理商失败');
            }
            
            return successResult('新增代理商成功');
        }
        
    }
    
    //获取代理商编辑信息
    public function getEditInfo() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        if($this->role != 1){
            return errorResult();
        }
        
        $agentId = request()->get('id', 0, 'intval');
        
        $field = ['agent_account', 'agent_name', 'agent_type', 'mch_id', 'mch_key', 'contact', 'mobile', 'service_telephone', 'withdra_rate', 'province_id', 'city_id', 'district_id', 'address', 'status'];
        
        $agent = model('Agent')->findOne(['agent_id' => $agentId], $field);
        
        if(empty($agent)) {
            return errorResult('代理商不存在');
        }
        
        return successResult($agent);
        
    }
    
    
    //设置密码
    public function setpwd() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 1){
            return errorResult('只有总后台才可以设置密码');
        }
        
        $params = request()->post();
        
        $password = isset($params['password']) ? trim($params['password']) : '';
        if(32 != strlen($password)){
            return errorResult('请输入正确的密码');
        }
        $password = strtolower($password);
        
        $agentId = isset($params['agentId']) ? intval($params['agentId']) : 0;
        if($agentId <= 0) {
            return errorResult();
        }
        
        $salt = uniqid();
        $salt = substr($salt, strlen($salt) -7 , 6);
        $password = md5(md5($password) . $salt);
        
        $rs = model('Agent')->modify(['agent_id' => $agentId], ['salt' => $salt, 'password' => $password]);
        if($rs) {
            return successResult('设置成功');
        }else{
            return errorResult('设置失败');
        }
    }
    
    public function dropSelect() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        if($this->role != 1){
            return errorResult();
        }
        
        $list = model('Agent')->findAll([], ['agent_id', 'agent_name']);
        return successResult($list);
    }
    
}


















